package homework;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;



public class number8 {
	public static void main(String[] args) {
	
		//准备牌
		ArrayList<String> poker=new ArrayList<String>();
		
		//定义一个哈希表存储大小
		HashMap<String, Integer> map = new HashMap<>();

		//定义一个value值区分大小
		int value = 2;
		//定义两个数组
		String[] colors={"♠","♣","♥","♦"}; 
		String[] numbers={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
		poker.add("大王");poker.add("小王");
		//将大小王添加入哈希表
		map.put("大王", 0);
		map.put("小王", 1);
		//组装52张牌
		for(String number:numbers){
			for(String color:colors){
				poker.add(color+number);
				map.put(color+number, value);
				value++;
			}
		}
		//展示所有牌
		System.out.println(poker);
		//展示牌值
		System.out.println(map);
		//洗牌
		Collections.shuffle(poker);
		
		Player playe1=new Player("老王");
		Player playe2=new Player("老李");
		Player playe3=new Player("老刘");
		//摸牌
		for(int i=0;i<poker.size();i++){
			//每一张牌
			String p = poker.get(i);
			if(i<=51){
				if(i%3==0){
					playe1.getList().add(p);
				}else if(i%3==1){
					playe2.getList().add(p);
				}else{
					playe3.getList().add(p);
				}
			}
		}
		
		
/*
		List<Integer> newList = new ArrayList();
		ArrayList<String> newList2=new ArrayList<String>();
		 for(String s :  playe1.getList()) {
	    	 int num = map.get(s);
	    	 newList.add(num);
	     }
		 Collections.sort(newList,Comparator.reverseOrder());
		for(int i : newList) {
			for (String s : map.keySet()) {
				if(i == map.get(s)) {
					newList2.add(s);
				}
			}
		}
*/
		 
	     

		//看牌
		System.out.println(playe1.getName()+":"+sortbyvalue(map, playe1.getList()));
		System.out.println(playe2.getName()+":"+sortbyvalue(map, playe2.getList()));
		System.out.println(playe3.getName()+":"+sortbyvalue(map, playe3.getList()));
	}
	
	//定义一个排序方法
	public static ArrayList<String> sortbyvalue(HashMap<String, Integer> map, List<String> list){
		//newlist作为判断牌大小的牌值列表
		List<Integer> newList = new ArrayList();
		//newlist2作为空列表存放转换的牌
		ArrayList<String> newList2=new ArrayList<String>();
		//将抽到的牌换成values
		 for(String s :  list) {
	    	 int num = map.get(s);
	    	 newList.add(num);
	     }
		 Collections.sort(newList,Comparator.reverseOrder());
		 //使用嵌套循环，先循环外部values值，再在哈希表中寻找该值的牌，将牌存入新列表中
		for(int i : newList) {
			for (String s : map.keySet()) {
				if(i == map.get(s)) {
					newList2.add(s);
				}
			}
		}
		return newList2;
    }
}
